#clean follow up study
#alan yan
#1-31-2022

####setup####
#clear environment
rm(list = ls())

#load libraries
pacman::p_load(tidyverse,
               DeclareDesign)

#load data
dt <- read.csv("02-framing-experiment/data/raw-data.csv", header = TRUE, stringsAsFactors = FALSE)

####functions###
standardizing <- function(data, variable) {
  temp <- data[[variable]]
  temp <- (temp - mean(temp, na.rm = TRUE))/sd(temp, na.rm = TRUE)
  return(temp)
}

####clean####
dt %>%
  filter(
    consent == "I agree to participate and I am at least 18 years old" &
      #this means that they passed the attention check and were randomly assigned
      codetermination != ""
  ) %>%
  mutate(
    age = (age - 18)/81,
    woman = ifelse(gender == 2, "Woman", "Man"),
    hhi_c = case_when(
      hhi == 1 ~ "Less than $14,999",
      hhi == 2 ~ "$15,000 to $19,999",
      hhi == 3 ~ "$20,000 to $24,999",
      hhi == 4 ~ "$25,000 to $29,999",
      hhi == 5 ~ "$30,000 to $34,999",
      hhi == 6 ~ "$35,000 to $39,999",
      hhi == 7 ~ "$40,000 to $44,999",
      hhi == 8 ~ "$45,000 to $49,999",
      hhi == 9 ~ "$50,000 to $54,999",
      hhi == 10	~ "$55,000 to $59,999",
      hhi == 11	~ "$60,000 to $64,999",
      hhi == 12	~ "$65,000 to $69,999",
      hhi == 13	~ "$70,000 to $74,999",
      hhi == 14	~ "$75,000 to $79,999",
      hhi == 15	~ "$80,000 to $84,999",
      hhi == 16	~ "$85,000 to $89,999",
      hhi == 17	~ "$90,000 to $94,999",
      hhi == 18	~ "$95,000 to $99,999",
      hhi == 19	~ "$100,000 to $124,999",
      hhi == 20	~ "$125,000 to $149,999",
      hhi == 21	~ "$150,000 to $174,999",
      hhi == 22	~ "$175,000 to $199,999",
      hhi == 23	~ "$200,000 to $249,999",
      hhi == 24	~ "$250,000 and above",
      hhi == -3105 ~ "Prefer not to answer"
    ),
    hhi = case_when(
      hhi %in% c(1:24) ~ (hhi - 1)/23,
      ),
    hhi = ifelse(is.na(hhi) == TRUE, mean(hhi, na.rm = TRUE), hhi),
    ethnicity = case_when(
      ethnicity == 1	~ "White", 
      ethnicity == 2	~ "Black",
      ethnicity == 3	~ "American Indian or Alaska Native",
      ethnicity == 4	~ "Asian",
      ethnicity == 5	~ "Asian",
      ethnicity == 6	~ "Asian",
      ethnicity == 7	~ "Asian",
      ethnicity == 8	~ "Asian",
      ethnicity == 9	~ "Asian",
      ethnicity == 10	~ "Asian",
      ethnicity == 11	~ "Pacific Islander",
      ethnicity == 12	~ "Pacific Islander",
      ethnicity == 13	~ "Pacific Islander",
      ethnicity == 14	~ "Pacific Islander",
      ethnicity == 15	~ "Some other race",
      ethnicity == 16	~ "Prefer not to answer" 
    ),
    ethnicity = ifelse(is.na(ethnicity) == TRUE, "Prefer not to answer", ethnicity),
    hispanic = case_when(
      hispanic == 1	~ "Not Hispanic",
      hispanic == 2	~ "Hispanic",
      hispanic == 3	~ "Hispanic",
      hispanic == 4	~ "Hispanic",
      hispanic == 5	~ "Hispanic",
      hispanic == 6	~ "Hispanic",
      hispanic == 7	~ "Hispanic",
      hispanic == 8	~ "Hispanic",
      hispanic == 9	~ "Hispanic",
      hispanic == 10 ~ "Hispanic",
      hispanic == 11 ~ "Hispanic",
      hispanic == 12 ~ "Hispanic",
      hispanic == 13 ~ "Hispanic",
      hispanic == 14 ~ "Hispanic",
      hispanic == 15 ~ "Prefer not to answer"
    ),
    hispanic = ifelse(is.na(hispanic) == TRUE, "Prefer not to answer", hispanic),
    education_c = case_when(
      education == 1 ~ "Some high school or less",
      education == 2 ~ "High school graduate",
      education == 3 ~ "Other post high school vocational training",
      education == 4 ~ "Completed some college, but no degree",
      education == 5 ~ "Associate's degree",
      education == 6 ~ "Bachelor's degree",
      education == 7 ~ "Master's or professional degree",
      education == 8 ~ "Doctorate degree",
      education == -3105 ~ "None of the above",
    ),
    education = case_when(
      education %in% c(1:8) ~ (education - 1)/7,
    ),
    education = ifelse(is.na(education) == TRUE, mean(education, na.rm = TRUE), education),
    political_party = case_when(
      political_party == 1 ~ 0,
      political_party == 2 ~ 1/6,
      political_party == 3 ~ 2/6,
      political_party == 4 ~ 3/6,
      political_party == 5 ~ 4/6,
      political_party == 6 ~ 2/6,
      political_party == 7 ~ 3/6,
      political_party == 8 ~ 4/6,
      political_party == 9 ~ 5/6,
      political_party == 10 ~	1
    ),
    political_party_c = case_when(
      political_party < 2/6 ~ "Democrat",
      political_party == 2/6 ~ "Lean Democrat",
      political_party == 3/6 ~ "Neither Democrat nor Republican",
      political_party == 4/6 ~ "Lean Republican",
      political_party > 4/6 ~ "Republican",
    ),
    political_party = ifelse(is.na(political_party), mean(political_party, na.rm = TRUE), political_party),
    political_party.centered = political_party - mean(political_party, na.rm = TRUE),
    region = case_when(
      region == 1	~ "Northeast",
      region == 2	~ "Midwest",
      region == 3	~ "South",
      region == 4	~ "West",
    ),
    region = ifelse(is.na(region) == TRUE, "Prefer not to answer", region),
    r_industry = ifelse(r_industry == "", "No answer", r_industry),
    r_occupation = ifelse(r_occupation == "", "No answer", r_occupation),
    #SDO
    sdo.1 = case_when(
      sdo.1 == "Strongly agree" ~ 0,
      sdo.1 == "Agree" ~ 1/6,
      sdo.1 == "Somewhat agree" ~ 2/6,
      sdo.1 == "Neither agree nor disagree" ~ 3/6,
      sdo.1 == "Somewhat disagree" ~ 4/6,
      sdo.1 == "Disagree" ~ 5/6,
      sdo.1 == "Strongly disagree" ~ 1,
    ),
    sdo.2 = case_when(
      sdo.2 == "Strongly agree" ~ 1,
      sdo.2 == "Agree" ~ 5/6,
      sdo.2 == "Somewhat agree" ~ 4/6,
      sdo.2 == "Neither agree nor disagree" ~ 3/6,
      sdo.2 == "Somewhat disagree" ~ 2/6,
      sdo.2 == "Disagree" ~ 1/6,
      sdo.2 == "Strongly disagree" ~ 0,
    ),
    sdo.3 = case_when(
      sdo.3 == "Strongly agree" ~ 0,
      sdo.3 == "Agree" ~ 1/6,
      sdo.3 == "Somewhat agree" ~ 2/6,
      sdo.3 == "Neither agree nor disagree" ~ 3/6,
      sdo.3 == "Somewhat disagree" ~ 4/6,
      sdo.3 == "Disagree" ~ 5/6,
      sdo.3 == "Strongly disagree" ~ 1,
    ),
    sdo.4 = case_when(
      sdo.4 == "Strongly agree" ~ 1,
      sdo.4 == "Agree" ~ 5/6,
      sdo.4 == "Somewhat agree" ~ 4/6,
      sdo.4 == "Neither agree nor disagree" ~ 3/6,
      sdo.4 == "Somewhat disagree" ~ 2/6,
      sdo.4 == "Disagree" ~ 1/6,
      sdo.4 == "Strongly disagree" ~ 0,
    ),
    sdo.index = (sdo.1 + sdo.2 + sdo.3 + sdo.4)/4,
    sdo.index = ifelse(is.na(sdo.index) == TRUE, mean(sdo.index, na.rm = TRUE), sdo.index),
    #political democracy
    democracy.1 = case_when(
      democracy.1 == "Strongly agree" ~ 0,
      democracy.1 == "Agree" ~ 1/6,
      democracy.1 == "Somewhat agree" ~ 2/6,
      democracy.1 == "Neither agree nor disagree" ~ 3/6,
      democracy.1 == "Somewhat disagree" ~ 4/6,
      democracy.1 == "Disagree" ~ 5/6,
      democracy.1 == "Strongly disagree" ~ 1,
    ),
    democracy.2 = case_when(
      democracy.2 == "Strongly agree" ~ 0,
      democracy.2 == "Agree" ~ 1/6,
      democracy.2 == "Somewhat agree" ~ 2/6,
      democracy.2 == "Neither agree nor disagree" ~ 3/6,
      democracy.2 == "Somewhat disagree" ~ 4/6,
      democracy.2 == "Disagree" ~ 5/6,
      democracy.2 == "Strongly disagree" ~ 1,
    ),
    democracy.3 = case_when(
      democracy.3 == "Strongly agree" ~ 0,
      democracy.3 == "Agree" ~ 1/6,
      democracy.3 == "Somewhat agree" ~ 2/6,
      democracy.3 == "Neither agree nor disagree" ~ 3/6,
      democracy.3 == "Somewhat disagree" ~ 4/6,
      democracy.3 == "Disagree" ~ 5/6,
      democracy.3 == "Strongly disagree" ~ 1,
    ),
    democracy.4 = case_when(
      democracy.4 == "Strongly agree" ~ 1,
      democracy.4 == "Agree" ~ 5/6,
      democracy.4 == "Somewhat agree" ~ 4/6,
      democracy.4 == "Neither agree nor disagree" ~ 3/6,
      democracy.4 == "Somewhat disagree" ~ 2/6,
      democracy.4 == "Disagree" ~ 1/6,
      democracy.4 == "Strongly disagree" ~ 0,
    ),
    democracy.4 = ifelse(is.na(democracy.4) == TRUE, mean(democracy.4, na.rm = TRUE), democracy.4),
    democracy.5 = case_when(
      democracy.5 == "Strongly agree" ~ 1,
      democracy.5 == "Agree" ~ 5/6,
      democracy.5 == "Somewhat agree" ~ 4/6,
      democracy.5 == "Neither agree nor disagree" ~ 3/6,
      democracy.5 == "Somewhat disagree" ~ 2/6,
      democracy.5 == "Disagree" ~ 1/6,
      democracy.5 == "Strongly disagree" ~ 0,
    ),
    democracy.index = (democracy.1 + democracy.2 + democracy.3 + democracy.4 + democracy.5)/5,
    democracy.index = ifelse(is.na(democracy.index) == TRUE, mean(democracy.index, na.rm = TRUE), democracy.index),
    #racial resentment 
    race.resen.1 = case_when(
      race.resen.1 == "Strongly agree" ~ 1,
      race.resen.1 == "Somewhat agree" ~ 3/4,
      race.resen.1 == "Neither agree nor disagree" ~ 2/4,
      race.resen.1 == "Somewhat disagree" ~ 1/4,
      race.resen.1 == "Strongly disagree" ~ 0,
    ),
    race.resen.2 = case_when(
      race.resen.2 == "Strongly agree" ~ 0,
      race.resen.2 == "Somewhat agree" ~ 1/4,
      race.resen.2 == "Neither agree nor disagree" ~ 2/4,
      race.resen.2 == "Somewhat disagree" ~ 3/4,
      race.resen.2 == "Strongly disagree" ~ 1,
    ),
    race_resen.index = (race.resen.1 + race.resen.2)/2,
    race_resen.index = ifelse(is.na(race_resen.index) == TRUE, mean(race_resen.index, na.rm = TRUE), race_resen.index),
    #class identity
    class.id.1 = case_when(
      class.id.1 == "Strongly agree" ~ 0,
      class.id.1 == "Agree" ~ 1/6,
      class.id.1 == "Somewhat agree" ~ 2/6,
      class.id.1 == "Neither agree nor disagree" ~ 3/6,
      class.id.1 == "Somewhat disagree" ~ 4/6,
      class.id.1 == "Disagree" ~ 5/6,
      class.id.1 == "Strongly disagree" ~ 1,
    ),
    class.id.3 = case_when(
      class.id.3 == "Strongly agree" ~ 1,
      class.id.3 == "Agree" ~ 5/6,
      class.id.3 == "Somewhat agree" ~ 4/6,
      class.id.3 == "Neither agree nor disagree" ~ 3/6,
      class.id.3 == "Somewhat disagree" ~ 2/6,
      class.id.3 == "Disagree" ~ 1/6,
      class.id.3 == "Strongly disagree" ~ 0,
    ),
    class.id.index = (class.id.1 + class.id.3)/2,
    class.id.index = ifelse(is.na(class.id.index) == TRUE, mean(class.id.index, na.rm = TRUE), class.id.index),
    #wright workplace power
    wright.index = case_when(
      wright.1 == "Non-management/Non-supervisory" & wright.2 == "No" & wright.3 == "No" ~ 0,
      wright.1 == "Non-management/Non-supervisory" & wright.2 == "Yes" & wright.3 == "No" ~ 1/8,
      wright.1 == "Non-management/Non-supervisory" & wright.2 == "No" & wright.3 == "Yes" ~ 1/8,
      wright.1 == "Supervisory" & wright.2 == "No" & wright.3 == "No" ~ 2/8,
      wright.1 == "Supervisory" & wright.2 == "Yes" & wright.3 == "No" ~ 3/8,
      wright.1 == "Supervisory" & wright.2 == "No" & wright.3 == "Yes" ~ 3/8,
      wright.1 == "Supervisory" & wright.2 == "Yes" & wright.3 == "Yes" ~ 4/8,
      #not sure how best to code up the managerial one
      #wright.1 == "Managerial" & wright.2 == "No" & wright.3 == "No" ~ 5,
      #wright.1 == "Managerial" & wright.2 == "Yes" & wright.3 == "No" ~ 6,
      #wright.1 == "Managerial" & wright.2 == "No" & wright.3 == "Yes" ~ 6,
      #wright.1 == "Managerial" & wright.2 == "Yes" & wright.3 == "Yes" ~ 7,
      wright.1 == "Managerial" & wright.1a == "Lower" ~ 5/8,
      wright.1 == "Managerial" & wright.1a == "Middle" ~ 6/8,
      wright.1 == "Managerial" & wright.1a == "Upper" ~ 7/8,
      wright.1 == "Managerial" & wright.1a == "Top" ~ 1,
    ),
    wright.index = ifelse(is.na(wright.index) == TRUE, mean(wright.index, na.rm = TRUE), wright.index),
    #political questions
    r_ideology = case_when(
      r_ideology == "Very conservative" ~ 1,
      r_ideology == "Somewhat conservative" ~ 5/6,
      r_ideology == "Slightly conservative" ~ 4/6,
      r_ideology == "Neither liberal nor conservative; middle of the road" ~ 3/6,
      r_ideology == "Slightly liberal" ~ 2/6,
      r_ideology == "Somewhat liberal" ~ 1/6,
      r_ideology == "Very liberal" ~ 0
    ),
    r_ideology = ifelse(is.na(r_ideology) == TRUE, mean(r_ideology, na.rm = TRUE), r_ideology),
    r_ideology.centered = r_ideology - mean(r_ideology, na.rm = TRUE),
    trump_support = case_when(
      trump_support == "Strongly approve" ~ 1,
      trump_support == "Somewhat approve" ~ 2/3,
      trump_support == "Somewhat disapprove" ~ 1/3,
      trump_support == "Strongly disapprove" ~ 0,
    ),
    trump_support = ifelse(is.na(trump_support) == TRUE, mean(trump_support, na.rm = TRUE), trump_support),
    vote = case_when(
      vote == "Yes" ~ 1,
      vote == "No" ~ 0,
    ),
    #codetermination
    support.codeterm = case_when(
      support.codeterm == "Strongly support" ~ 1,
      support.codeterm == "Somewhat support" ~ 5/6,
      support.codeterm == "Slightly support" ~ 4/6,
      support.codeterm == "Neither support nor oppose" ~ 3/6,
      support.codeterm == "Slightly oppose" ~ 2/6,
      support.codeterm == "Somewhat oppose" ~ 1/6,
      support.codeterm == "Strongly oppose" ~ 0,
    ),
    support.codeterm = ifelse(is.na(support.codeterm) == TRUE, mean(support.codeterm, na.rm = TRUE), support.codeterm),
    work.codeterm = case_when(
      work.codeterm == "Very likely" ~ 1,
      work.codeterm == "Somewhat likely" ~ 5/6,
      work.codeterm == "Slightly likely" ~ 4/6,
      work.codeterm == "Neither likely nor unlikely" ~ 3/6,
      work.codeterm == "Slightly unlikely" ~ 2/6,
      work.codeterm == "Somewhat unlikely" ~ 1/6,
      work.codeterm == "Very unlikely" ~ 0,
    ),
    work.codeterm = ifelse(is.na(work.codeterm) == TRUE, mean(work.codeterm, na.rm = TRUE), work.codeterm),
    power.codeterm = case_when(
      power.codeterm == "Much more power" ~ 1,
      power.codeterm == "Somewhat more power" ~ 5/6,
      power.codeterm == "Slightly more power" ~ 4/6,
      power.codeterm == "The same amount of power they have now" ~ 3/6,
      power.codeterm == "Slightly less power" ~ 2/6,
      power.codeterm == "Somewhat less power" ~ 1/6,
      power.codeterm == "Much less power" ~ 0,
    ),
    power.codeterm = ifelse(is.na(power.codeterm) == TRUE, mean(power.codeterm, na.rm = TRUE), power.codeterm),
    resp.codeterm = case_when(
      resp.codeterm == "Much more time" ~ 1,
      resp.codeterm == "Somewhat more time" ~ 5/6,
      resp.codeterm == "Slightly more time" ~ 4/6,
      resp.codeterm == "The same amount of time they spend now" ~ 3/6,
      resp.codeterm == "Slightly less time" ~ 2/6,
      resp.codeterm == "Somewhat less time" ~ 1/6,
      resp.codeterm == "Much less time" ~ 0,
    ),
    resp.codeterm = ifelse(is.na(resp.codeterm) == TRUE, mean(resp.codeterm, na.rm = TRUE), resp.codeterm),
    manip.check.codeterm.recode = ifelse(manip.check.codeterm == "Codetermination is where workers elect representatives to sit on the corporate board", 1, 0),
    #manager elections
    support.elections = case_when(
      support.elections == "Strongly support" ~ 1,
      support.elections == "Somewhat support" ~ 5/6,
      support.elections == "Slightly support" ~ 4/6,
      support.elections == "Neither support nor oppose" ~ 3/6,
      support.elections == "Slightly oppose" ~ 2/6,
      support.elections == "Somewhat oppose" ~ 1/6,
      support.elections == "Strongly oppose" ~ 0,
    ),
    support.elections = ifelse(is.na(support.elections) == TRUE, mean(support.elections, na.rm = TRUE), support.elections),
    work.elections = case_when(
      work.elections == "Very likely" ~ 1,
      work.elections == "Somewhat likely" ~ 5/6,
      work.elections == "Slightly likely" ~ 4/6,
      work.elections == "Neither likely nor unlikely" ~ 3/6,
      work.elections == "Slightly unlikely" ~ 2/6,
      work.elections == "Somewhat unlikely" ~ 1/6,
      work.elections == "Very unlikely" ~ 0,
    ),
    work.elections = ifelse(is.na(work.elections) == TRUE, mean(work.elections, na.rm = TRUE), work.elections),
    power.elections = case_when(
      power.elections == "Much more power" ~ 1,
      power.elections == "Somewhat more power" ~ 5/6,
      power.elections == "Slightly more power" ~ 4/6,
      power.elections == "The same amount of power they have now" ~ 3/6,
      power.elections == "Slightly less power" ~ 2/6,
      power.elections == "Somewhat less power" ~ 1/6,
      power.elections == "Much less power" ~ 0,
    ),
    power.elections = ifelse(is.na(power.elections) == TRUE, mean(power.elections, na.rm = TRUE), power.elections),
    resp.elections = case_when(
      resp.elections == "Much more time" ~ 1,
      resp.elections == "Somewhat more time" ~ 5/6,
      resp.elections == "Slightly more time" ~ 4/6,
      resp.elections == "The same amount of time they spend now" ~ 3/6,
      resp.elections == "Slightly less time" ~ 2/6,
      resp.elections == "Somewhat less time" ~ 1/6,
      resp.elections == "Much less time" ~ 0,
    ),
    resp.elections = ifelse(is.na(resp.elections) == TRUE, mean(resp.elections, na.rm = TRUE), resp.elections),
    manip.check.election.recode = ifelse(manip.check.election == "Manager elections are where workers vote for their managers", 1, 0),
    #ESOPs
    support.esop = case_when(
      support.esop == "Strongly support" ~ 1,
      support.esop == "Somewhat support" ~ 5/6,
      support.esop == "Slightly support" ~ 4/6,
      support.esop == "Neither support nor oppose" ~ 3/6,
      support.esop == "Slightly oppose" ~ 2/6,
      support.esop == "Somewhat oppose" ~ 1/6,
      support.esop == "Strongly oppose" ~ 0,
    ),
    support.esop = ifelse(is.na(support.esop) == TRUE, mean(support.esop, na.rm = TRUE), support.esop),
    work.esop = case_when(
      work.esop == "Very likely" ~ 1,
      work.esop == "Somewhat likely" ~ 5/6,
      work.esop == "Slightly likely" ~ 4/6,
      work.esop == "Neither likely nor unlikely" ~ 3/6,
      work.esop == "Slightly unlikely" ~ 2/6,
      work.esop == "Somewhat unlikely" ~ 1/6,
      work.esop == "Very unlikely" ~ 0,
    ),
    work.esop = ifelse(is.na(work.esop) == TRUE, mean(work.esop, na.rm = TRUE), work.esop),
    power.esop = case_when(
      power.esop == "Much more power" ~ 1,
      power.esop == "Somewhat more power" ~ 5/6,
      power.esop == "Slightly more power" ~ 4/6,
      power.esop == "The same amount of power they have now" ~ 3/6,
      power.esop == "Slightly less power" ~ 2/6,
      power.esop == "Somewhat less power" ~ 1/6,
      power.esop == "Much less power" ~ 0,
    ),
    power.esop = ifelse(is.na(power.esop) == TRUE, mean(power.esop, na.rm = TRUE), power.esop),
    resp.esop = case_when(
      resp.esop == "Much more time" ~ 1,
      resp.esop == "Somewhat more time" ~ 5/6,
      resp.esop == "Slightly more time" ~ 4/6,
      resp.esop == "The same amount of time they spend now" ~ 3/6,
      resp.esop == "Slightly less time" ~ 2/6,
      resp.esop == "Somewhat less time" ~ 1/6,
      resp.esop == "Much less time" ~ 0,
    ),
    resp.esop = ifelse(is.na(resp.esop) == TRUE, mean(resp.esop, na.rm = TRUE), resp.esop),
    manip.check.esop.recode = ifelse(manip.check.esop == "ESOPs are where workers accrue stock in the companies they work for", 1, 0),
    #Fracking
    support.enviro = case_when(
      support.enviro == "Strongly support" ~ 1,
      support.enviro == "Somewhat support" ~ 5/6,
      support.enviro == "Slightly support" ~ 4/6,
      support.enviro == "Neither support nor oppose" ~ 3/6,
      support.enviro == "Slightly oppose" ~ 2/6,
      support.enviro == "Somewhat oppose" ~ 1/6,
      support.enviro == "Strongly oppose" ~ 0,
    ),
    support.enviro = ifelse(is.na(support.enviro) == TRUE, mean(support.enviro, na.rm = TRUE), support.enviro),
    perceive.cost.enviro = case_when(
      perceive.cost.enviro == "A great deal" ~ 1,
      perceive.cost.enviro == "A lot" ~ 3/4,
      perceive.cost.enviro == "A moderate amount" ~ 2/4,
      perceive.cost.enviro == "A little" ~ 1/4,
      perceive.cost.enviro == "Nothing at all" ~ 0,
    ),
    perceive.cost.enviro = ifelse(is.na(perceive.cost.enviro) == TRUE, mean(perceive.cost.enviro, na.rm = TRUE), perceive.cost.enviro),
    perceive.ben.enviro = case_when(
      perceive.ben.enviro == "A great deal" ~ 1,
      perceive.ben.enviro == "A lot" ~ 3/4,
      perceive.ben.enviro == "A moderate amount" ~ 2/4,
      perceive.ben.enviro == "A little" ~ 1/4,
      perceive.ben.enviro == "Nothing at all" ~ 0,
    ),
    perceive.ben.enviro = ifelse(is.na(perceive.ben.enviro) == TRUE, mean(perceive.ben.enviro, na.rm = TRUE), perceive.ben.enviro),
    manip.check.enviro.recode = ifelse(manip.check.enviro == "A ban on fracking would prohibit shooting water into the ground to extract oil and gas", 1, 0), 
    #paid family leave
    support.parent = case_when(
      support.parent == "Strongly support" ~ 1,
      support.parent == "Somewhat support" ~ 5/6,
      support.parent == "Slightly support" ~ 4/6,
      support.parent == "Neither support nor oppose" ~ 3/6,
      support.parent == "Slightly oppose" ~ 2/6,
      support.parent == "Somewhat oppose" ~ 1/6,
      support.parent == "Strongly oppose" ~ 0,
    ),
    support.parent = ifelse(is.na(support.parent) == TRUE, mean(support.parent, na.rm = TRUE), support.parent),
    work.parent = case_when(
      work.parent == "Very likely" ~ 1,
      work.parent == "Somewhat likely" ~ 5/6,
      work.parent == "Slightly likely" ~ 4/6,
      work.parent == "Neither likely nor unlikely" ~ 3/6,
      work.parent == "Slightly unlikely" ~ 2/6,
      work.parent == "Somewhat unlikely" ~ 1/6,
      work.parent == "Very unlikely" ~ 0,
    ),
    work.parent = ifelse(is.na(work.parent) == TRUE, mean(work.parent, na.rm = TRUE), work.parent),
    perceive.cost.parent = case_when(
      perceive.cost.parent == "A great deal" ~ 1,
      perceive.cost.parent == "A lot" ~ 3/4,
      perceive.cost.parent == "A moderate amount" ~ 2/4,
      perceive.cost.parent == "A little" ~ 1/4,
      perceive.cost.parent == "Nothing at all" ~ 0,
    ),
    perceive.cost.parent = ifelse(is.na(perceive.cost.parent) == TRUE, mean(perceive.cost.parent, na.rm = TRUE), perceive.cost.parent),
    perceive.ben.parent = case_when(
      perceive.ben.parent == "A great deal" ~ 1,
      perceive.ben.parent == "A lot" ~ 3/4,
      perceive.ben.parent == "A moderate amount" ~ 2/4,
      perceive.ben.parent == "A little" ~ 1/4,
      perceive.ben.parent == "Nothing at all" ~ 0,
    ),
    perceive.ben.parent = ifelse(is.na(perceive.ben.parent) == TRUE, mean(perceive.ben.parent, na.rm = TRUE), perceive.ben.parent),
    manip.check.parent.recode = ifelse(manip.check.parent == "Workers would have four weeks of paid leave for new motherhood or caring for a loved one", 1, 0), 
    policy_order.randomization.codetermination = case_when(
      FL_10_DO == "codetermination|managerelections|ESOPs" ~ "First",
      FL_10_DO == "codetermination|ESOPs|managerelections" ~ "First",
      FL_10_DO == "managerelections|codetermination|ESOPs" ~ "Second",
      FL_10_DO == "ESOPs|codetermination|managerelections" ~ "Second",
      FL_10_DO == "managerelections|ESOPs|codetermination" ~ "Third",
      FL_10_DO == "ESOPs|managerelections|codetermination" ~ "Third",
    ),
    policy_order.randomization.elections = case_when(
      FL_10_DO == "codetermination|managerelections|ESOPs" ~ "Second",
      FL_10_DO == "codetermination|ESOPs|managerelections" ~ "Third",
      FL_10_DO == "managerelections|codetermination|ESOPs" ~ "First",
      FL_10_DO == "ESOPs|codetermination|managerelections" ~ "Third",
      FL_10_DO == "managerelections|ESOPs|codetermination" ~ "First",
      FL_10_DO == "ESOPs|managerelections|codetermination" ~ "Second",
    ),
    policy_order.randomization.esops = case_when(
      FL_10_DO == "codetermination|managerelections|ESOPs" ~ "Third",
      FL_10_DO == "codetermination|ESOPs|managerelections" ~ "Second",
      FL_10_DO == "managerelections|codetermination|ESOPs" ~ "Third",
      FL_10_DO == "ESOPs|codetermination|managerelections" ~ "First",
      FL_10_DO == "managerelections|ESOPs|codetermination" ~ "Second",
      FL_10_DO == "ESOPs|managerelections|codetermination" ~ "First",
    ),
    policy_order.randomization.enviro = case_when(
      FL_10_DO == "environmentalpolicy|parentalleave" ~ "First",
      FL_10_DO == "parentalleave|environmentalpolicy" ~ "Second",
    ),
    policy_order.randomization.parent = case_when(
      FL_10_DO == "environmentalpolicy|parentalleave" ~ "Second",
      FL_10_DO == "parentalleave|environmentalpolicy" ~ "First",
    ),
    rid = 1:nrow(.)
  ) %>% 
  select(
    consent,
    attention.check.1,
    attention.check.2,
    sdo.1,
    sdo.2,
    sdo.3,
    sdo.4,
    democracy.1,
    democracy.2,
    democracy.3,
    democracy.4,
    democracy.5,
    race.resen.1,
    race.resen.2,
    r_industry,
    r_occupation,
    r_union,
    r_class,
    class.id.1,
    class.id.3,
    wright.1,
    wright.1a,
    wright.2,
    wright.3,
    r_ideology,
    r_ideology.centered,
    trump_support,
    vote,
    vote_choice_2020,
    support.codeterm,
    work.codeterm,
    power.codeterm,
    resp.codeterm,
    manip.check.codeterm,
    manip.check.codeterm.recode,
    support.elections,
    work.elections,
    power.elections,
    resp.elections,
    manip.check.election,
    manip.check.election.recode,
    support.esop,
    work.esop,
    power.esop,
    resp.esop,
    manip.check.esop,
    manip.check.esop.recode,
    support.enviro,
    perceive.cost.enviro,
    perceive.ben.enviro,
    manip.check.enviro,
    manip.check.enviro.recode,
    support.parent,
    work.parent,
    perceive.cost.parent,
    perceive.ben.parent,
    manip.check.parent,
    manip.check.parent.recode,
    rid,
    age,
    gender,
    hhi,
    hhi_c,
    ethnicity,
    hispanic,
    education,
    education_c,
    political_party,
    political_party.centered,
    political_party_c,
    region,
    zip,
    democracy_sum,
    sdo_sum,
    block,
    codetermination,
    manager.elections,
    esops,
    enviro,
    parent,
    woman,
    sdo.index,
    democracy.index,
    race_resen.index,
    class.id.index,
    wright.index,
    policy_order.randomization.codetermination,
    policy_order.randomization.elections,
    policy_order.randomization.esops,
    policy_order.randomization.enviro,
    policy_order.randomization.parent
  ) -> dt.individual_policies

#standardize variables
dt.individual_policies %>%
  mutate(
    support.codeterm.standardized = standardizing(dt.individual_policies, "support.codeterm"),
    work.codeterm.standardized = standardizing(dt.individual_policies, "work.codeterm"),
    power.codeterm.standardized = standardizing(dt.individual_policies, "power.codeterm"),
    resp.codeterm.standardized = standardizing(dt.individual_policies, "resp.codeterm"),
    support.elections.standardized = standardizing(dt.individual_policies, "support.elections"),
    work.elections.standardized = standardizing(dt.individual_policies, "work.elections"),
    power.elections.standardized = standardizing(dt.individual_policies, "power.elections"),
    resp.elections.standardized = standardizing(dt.individual_policies, "resp.elections"),
    support.esop.standardized = standardizing(dt.individual_policies, "support.esop"),
    work.esop.standardized = standardizing(dt.individual_policies, "work.esop"),
    power.esop.standardized = standardizing(dt.individual_policies, "power.esop"),
    resp.esop.standardized = standardizing(dt.individual_policies, "resp.esop"),
    support.enviro.standardized = standardizing(dt.individual_policies, "support.enviro"),
    perceive.cost.enviro.standardized = standardizing(dt.individual_policies, "perceive.cost.enviro"),
    perceive.ben.enviro.standardized = standardizing(dt.individual_policies, "perceive.ben.enviro"),
    support.parent.standardized = standardizing(dt.individual_policies, "support.parent"),
    work.parent.standardized = standardizing(dt.individual_policies, "work.parent"),
    perceive.cost.parent.standardized = standardizing(dt.individual_policies, "perceive.cost.parent"),
    perceive.ben.parent.standardized = standardizing(dt.individual_policies, "perceive.ben.parent"),
    democracy.index.centered = democracy.index - mean(democracy.index, na.rm = TRUE),
    wright.index.centered = wright.index - mean(wright.index, na.rm = TRUE),
    class.id.index.centered = class.id.index - mean(class.id.index, na.rm = TRUE),
    sdo.index.centered = sdo.index - mean(sdo.index, na.rm = TRUE),
  ) -> dt.individual_policies

#write_rds(dt.individual_policies, "08-reviews/experiment/data/clean-data-indiv-pol")

####construct the stacked datasets####
#create policy specific datasets first
#### codeterm ####
dt.individual_policies %>%
  select(
    consent,
    attention.check.1,
    attention.check.2,
    sdo.1,
    sdo.2,
    sdo.3,
    sdo.4,
    democracy.1,
    democracy.2,
    democracy.3,
    democracy.4,
    democracy.5,
    race.resen.1,
    race.resen.2,
    r_industry,
    r_occupation,
    r_union,
    r_class,
    class.id.1,
    class.id.3,
    wright.1,
    wright.1a,
    wright.2,
    wright.3,
    r_ideology,
    r_ideology.centered,
    trump_support,
    vote,
    vote_choice_2020,
    support.codeterm,
    work.codeterm,
    power.codeterm,
    resp.codeterm,
    manip.check.codeterm,
    manip.check.codeterm.recode,
    support.codeterm.standardized,
    work.codeterm.standardized,
    power.codeterm.standardized,
    resp.codeterm.standardized,
    rid,
    age,
    gender,
    hhi,
    hhi_c,
    ethnicity,
    hispanic,
    education,
    education_c,
    political_party,
    political_party.centered,
    political_party_c,
    region,
    zip,
    democracy_sum,
    sdo_sum,
    block,
    codetermination,
    woman,
    sdo.index,
    democracy.index,
    race_resen.index,
    class.id.index,
    wright.index,
    democracy.index.centered,
    wright.index.centered,
    class.id.index.centered,
    sdo.index.centered,
    policy_order.randomization.codetermination,
    policy_order.randomization.elections,
    policy_order.randomization.esops,
    policy_order.randomization.enviro,
    policy_order.randomization.parent,
  ) %>%
  rename(
    support = support.codeterm,
    work = work.codeterm,
    power = power.codeterm,
    resp = resp.codeterm,
    support.standardized = support.codeterm.standardized,
    work.standardized = work.codeterm.standardized,
    power.standardized = power.codeterm.standardized,
    resp.standardized = resp.codeterm.standardized,
    treatment = codetermination,
    manip.check = manip.check.codeterm.recode,
  ) %>%
  mutate(
    policy = "codeterm",
    cost = NA,
    ben = NA,
    cost.standardized = NA,
    ben.standardized = NA,
    workplace_democracy_policy = 1,
    manip.check.election = NA,
    manip.check.esop = NA,
    manip.check.enviro = NA,
    manip.check.parent = NA
  ) -> dt.codetermination

##### elections ####
dt.individual_policies %>%
  select(
    consent,
    attention.check.1,
    attention.check.2,
    sdo.1,
    sdo.2,
    sdo.3,
    sdo.4,
    democracy.1,
    democracy.2,
    democracy.3,
    democracy.4,
    democracy.5,
    race.resen.1,
    race.resen.2,
    r_industry,
    r_occupation,
    r_union,
    r_class,
    class.id.1,
    class.id.3,
    wright.1,
    wright.1a,
    wright.2,
    wright.3,
    r_ideology,
    r_ideology.centered,
    trump_support,
    vote,
    vote_choice_2020,
    support.elections,
    work.elections,
    power.elections,
    resp.elections,
    manip.check.election,
    manip.check.election.recode,
    support.elections.standardized,
    work.elections.standardized,
    power.elections.standardized,
    resp.elections.standardized,
    rid,
    age,
    gender,
    hhi,
    hhi_c,
    ethnicity,
    hispanic,
    education,
    education_c,
    political_party,
    political_party.centered,
    political_party_c,
    region,
    zip,
    democracy_sum,
    sdo_sum,
    block,
    manager.elections,
    woman,
    sdo.index,
    democracy.index,
    race_resen.index,
    class.id.index,
    wright.index,
    democracy.index.centered,
    wright.index.centered,
    class.id.index.centered,
    sdo.index.centered,
    policy_order.randomization.codetermination,
    policy_order.randomization.elections,
    policy_order.randomization.esops,
    policy_order.randomization.enviro,
    policy_order.randomization.parent,
  ) %>%
  rename(
    support = support.elections,
    work = work.elections,
    power = power.elections,
    resp = resp.elections,
    support.standardized = support.elections.standardized,
    work.standardized = work.elections.standardized,
    power.standardized = power.elections.standardized,
    resp.standardized = resp.elections.standardized,
    treatment = manager.elections,
    manip.check = manip.check.election.recode,
  ) %>%
  mutate(
    policy = "elections",
    cost = NA,
    ben = NA,
    cost.standardized = NA,
    ben.standardized = NA,
    workplace_democracy_policy = 1,
    manip.check.codeterm = NA,
    manip.check.esop = NA,
    manip.check.enviro = NA,
    manip.check.parent = NA
  ) -> dt.elections

#### esops ####
dt.individual_policies %>%
  select(
    consent,
    attention.check.1,
    attention.check.2,
    sdo.1,
    sdo.2,
    sdo.3,
    sdo.4,
    democracy.1,
    democracy.2,
    democracy.3,
    democracy.4,
    democracy.5,
    race.resen.1,
    race.resen.2,
    r_industry,
    r_occupation,
    r_union,
    r_class,
    class.id.1,
    class.id.3,
    wright.1,
    wright.1a,
    wright.2,
    wright.3,
    r_ideology,
    r_ideology.centered,
    trump_support,
    vote,
    vote_choice_2020,
    support.esop,
    work.esop,
    power.esop,
    resp.esop,
    manip.check.esop,
    manip.check.esop.recode,
    support.esop.standardized,
    work.esop.standardized,
    power.esop.standardized,
    resp.esop.standardized,
    rid,
    age,
    gender,
    hhi,
    hhi_c,
    ethnicity,
    hispanic,
    education,
    education_c,
    political_party,
    political_party.centered,
    political_party_c,
    region,
    zip,
    democracy_sum,
    sdo_sum,
    block,
    esops,
    woman,
    sdo.index,
    democracy.index,
    race_resen.index,
    class.id.index,
    wright.index,
    democracy.index.centered,
    wright.index.centered,
    class.id.index.centered,
    sdo.index.centered,
    policy_order.randomization.codetermination,
    policy_order.randomization.elections,
    policy_order.randomization.esops,
    policy_order.randomization.enviro,
    policy_order.randomization.parent,
  ) %>%
  rename(
    support = support.esop,
    work = work.esop,
    power = power.esop,
    resp = resp.esop,
    support.standardized = support.esop.standardized,
    work.standardized = work.esop.standardized,
    power.standardized = power.esop.standardized,
    resp.standardized = resp.esop.standardized,
    treatment = esops,
    manip.check = manip.check.esop.recode
  ) %>%
  mutate(
    policy = "esops",
    cost = NA,
    ben = NA,
    cost.standardized = NA,
    ben.standardized = NA,
    workplace_democracy_policy = 1,
    manip.check.codeterm = NA,
    manip.check.election = NA,
    manip.check.enviro = NA,
    manip.check.parent = NA
  ) -> dt.esops

#### enviro ####
dt.individual_policies %>%
  select(
    consent,
    attention.check.1,
    attention.check.2,
    sdo.1,
    sdo.2,
    sdo.3,
    sdo.4,
    democracy.1,
    democracy.2,
    democracy.3,
    democracy.4,
    democracy.5,
    race.resen.1,
    race.resen.2,
    r_industry,
    r_occupation,
    r_union,
    r_class,
    class.id.1,
    class.id.3,
    wright.1,
    wright.1a,
    wright.2,
    wright.3,
    r_ideology,
    r_ideology.centered,
    trump_support,
    vote,
    vote_choice_2020,
    support.enviro,
    perceive.cost.enviro,
    perceive.ben.enviro,
    manip.check.enviro,
    manip.check.enviro.recode,
    support.enviro.standardized,
    perceive.cost.enviro.standardized,
    perceive.ben.enviro.standardized,
    rid,
    age,
    gender,
    hhi,
    hhi_c,
    ethnicity,
    hispanic,
    education,
    education_c,
    political_party,
    political_party.centered,
    political_party_c,
    region,
    zip,
    democracy_sum,
    sdo_sum,
    block,
    enviro,
    woman,
    sdo.index,
    democracy.index,
    race_resen.index,
    class.id.index,
    wright.index,
    democracy.index.centered,
    wright.index.centered,
    class.id.index.centered,
    sdo.index.centered,
    policy_order.randomization.codetermination,
    policy_order.randomization.elections,
    policy_order.randomization.esops,
    policy_order.randomization.enviro,
    policy_order.randomization.parent,
  ) %>%
  rename(
    support = support.enviro,
    cost = perceive.cost.enviro,
    ben = perceive.ben.enviro,
    support.standardized = support.enviro.standardized,
    cost.standardized = perceive.cost.enviro.standardized,
    ben.standardized = perceive.ben.enviro.standardized,
    treatment = enviro,
    manip.check = manip.check.enviro.recode
  ) %>%
  mutate(
    policy = "enviro",
    work = NA,
    power = NA,
    resp = NA,
    work.standardized = NA,
    power.standardized = NA,
    resp.standardized = NA,
    workplace_democracy_policy = 0,
    manip.check.codeterm = NA,
    manip.check.election = NA,
    manip.check.esop = NA,
    manip.check.parent = NA
  ) -> dt.enviro

#### parental leave ####
dt.individual_policies %>%
  select(
    consent,
    attention.check.1,
    attention.check.2,
    sdo.1,
    sdo.2,
    sdo.3,
    sdo.4,
    democracy.1,
    democracy.2,
    democracy.3,
    democracy.4,
    democracy.5,
    race.resen.1,
    race.resen.2,
    r_industry,
    r_occupation,
    r_union,
    r_class,
    class.id.1,
    class.id.3,
    wright.1,
    wright.1a,
    wright.2,
    wright.3,
    r_ideology,
    r_ideology.centered,
    trump_support,
    vote,
    vote_choice_2020,
    support.parent,
    work.parent,
    perceive.cost.parent,
    perceive.ben.parent,
    manip.check.parent,
    manip.check.parent.recode,
    support.parent.standardized,
    work.parent.standardized,
    perceive.cost.parent.standardized,
    perceive.ben.parent.standardized,
    rid,
    age,
    gender,
    hhi,
    hhi_c,
    ethnicity,
    hispanic,
    education,
    education_c,
    political_party,
    political_party.centered,
    political_party_c,
    region,
    zip,
    democracy_sum,
    sdo_sum,
    block,
    parent,
    woman,
    sdo.index,
    democracy.index,
    race_resen.index,
    class.id.index,
    wright.index,
    democracy.index.centered,
    wright.index.centered,
    class.id.index.centered,
    sdo.index.centered,
    policy_order.randomization.codetermination,
    policy_order.randomization.elections,
    policy_order.randomization.esops,
    policy_order.randomization.enviro,
    policy_order.randomization.parent,
  ) %>%
  rename(
    support = support.parent,
    work = work.parent,
    cost = perceive.cost.parent,
    ben = perceive.ben.parent,
    support.standardized = support.parent.standardized,
    work.standardized = work.parent.standardized,
    cost.standardized = perceive.cost.parent.standardized,
    ben.standardized = perceive.ben.parent.standardized,
    treatment = parent,
    manip.check = manip.check.parent.recode
  ) %>%
  mutate(
    policy = "parent",
    power = NA,
    resp = NA,
    power.standardized = NA,
    resp.standardized = NA,
    workplace_democracy_policy = 0,
    manip.check.codeterm = NA,
    manip.check.election = NA,
    manip.check.esop = NA,
    manip.check.enviro = NA
  ) -> dt.parent

#bind together the individual policies
rbind(
  dt.codetermination,
  dt.elections,
  dt.esops,
  dt.enviro,
  dt.parent
) %>%
  mutate(
    treatment = factor(treatment,
                       levels = c("control",
                                  "benefit",
                                  "cost",
                                  "both"))
  ) -> dt.stacked_policies

#write_rds(dt.stacked_policies, "02-framing-experiment/data/clean-data-stacked-pol")
